<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor
        :itemList="[
          { name: 'box-base', title: '基础信息' },
          { name: 'box-process', title: '加工信息' },
          { name: 'box-list', title: '加工单列表' },
        ]"
      />
    </div>

    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">基础信息</div>
          <customFormItem label="品名" name="matrName" :parent="_self" :form="form" disabled/>
          <customFormItem label="品规" name="matrSpec" :parent="_self" :form="form" disabled/>
          <customFormItem label="库位" name="locationName" :parent="_self" :form="form" disabled/>
          <customFormItem label="批次" name="lot" :parent="_self" :form="form" disabled/>
          <customFormItem label="效期" name="expDate" :parent="_self" :form="form" disabled/>
          <customFormItem label="生产日期" name="mfgDate" :parent="_self" :form="form" disabled/>
          <customFormItem label="sn" name="sn" :parent="_self" :form="form" disabled/>
          <customFormItem label="rfid" name="rfid" :parent="_self" :form="form" disabled/>
          <customFormItem label="金额" name="amount" :parent="_self" :form="form" disabled/>

          <div id="box-process" class="box-title">加工信息</div>
          <div class="label-class w-280">
            <strong>加工类型</strong>{{ cpProcessType }}
          </div>
          <customFormItem label="加工单号" name="busKey" :parent="_self" :form="form" disabled/>
          <customFormItem label="加工仓库" name="warehouseName" :parent="_self" :form="form" disabled/>
          <customFormItem label="加工前包装" name="packageUnit" :parent="_self" :form="form" disabled/>
          <customFormItem label="加工后包装" name="packageProcess" :parent="_self" :form="form" disabled/>
          <customFormItem label="加工数量" name="processQty" :parent="_self" :form="form" disabled/>
        </template>
      </customForm>

      <div id="box-list" class="box-title">加工单列表</div>
      <data-view
       ref="childDataView"
       propViewKey="wm_stock_process_item"
       :propPreQueryWrapper="cpChildViewQueryWrapper"
       propHideAllButton="true"
       :propAutoFetchData="false"  />
    </div>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'

export default {
  data() {
    return {
      form: {},
      viewData: {}
    }
  },
  created() {
    let that = this
    this.$emit('event-register', {
      validate: callback => {
        return this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
      },
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {},
      beforeFlow: flowButtonKey => {
        return true
      },
      afterFlow: flowButtonKey => {},
      beforeClose: () => {
        return true
      },
      async afterClick(button, data, viewForm) {
        let {key: buttonKey} = button
        if (buttonKey == 'print') { //标签打印
          let snArr = []
          if (that.form.processType == '1') { //拆分
            let wrapper = global.$newWrapper().eq('process_id', that.form.id)
            const qString = JSON.stringify(wrapper.queryConditions)
            const params = {
              queryString: qString,
            }
            await global.$ajax.get('/wm/stock_process_item/list', {params}).then(response => {
              if (response.code == 0) {
                if (response.data.length > 0) {
                  response.data.forEach(ob => {
                    snArr.push(ob.sn)
                  })
                }
              }
            })
          } else {
            snArr = [that.form.sn]
          }
          global.utilPrint.openPrintLabel(null, null, snArr)
        }
      }
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object
  },
  components: {customAnchor},
  computed: {
    cpChildViewQueryWrapper() {
      let wrapper = this.$newWrapper()
      wrapper.eq('processId', this.form.id)
      return wrapper
    },
    cpProcessType() {
      if (!this.propDictionaries || !this.form.processType || this.propDictionaries.length == 0) {
        return ''
      }
      return this.propDictionaries.processType.find(item => item.value == this.form.processType).label
    }
  },
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100%;
}
</style>
